package forms;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class formGPA  extends ActionForm {
	private static final long serialVersionUID = 1L;
	private String gpa = null;

	public String getGpa() {
		return gpa;
	}

	public boolean isNumeric(String name) { // Allows decimals
		try {
			Double.parseDouble(name);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setGpa(null);
	}

	public void setGpa(String gpa) {
		this.gpa = gpa;
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors(); // x.x or x and must be < 4
		if ((gpa == null) || (gpa.length() < 1) || (gpa.length() > 3)
				|| (gpa.length() == 2) || (Double.parseDouble(gpa) > 4.0)
				|| (!isNumeric(gpa))) {
			errors.add("firstMsgTag1", new ActionMessage("errors.required",
					"GPA"));
		}
		return errors;
	}
}
